package com.example.demo;

import com.majie.jedis.EnableJedisPool;
import com.majie.jedis.JedisService;
import com.majie.qiniu.QiNiuYunService;
import com.qiniu.http.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@SpringBootApplication
@RestController
@EnableJedisPool
public class Demo1Application {

    private final QiNiuYunService qiNiuYunService;
    private final JedisService jedisService;

    @Autowired
    public Demo1Application(QiNiuYunService qiNiuYunService, JedisService jedisService) {
        this.qiNiuYunService = qiNiuYunService;
        this.jedisService = jedisService;
    }

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Object uploadPhoto(@RequestParam("file") MultipartFile file) throws IOException {
//		String fileName = file.getOriginalFilename();

        InputStream inputStream = file.getInputStream();
        Response response = qiNiuYunService.uploadFile(inputStream);

        return response;
    }

    @PostMapping("/redis")
    public Object set(@RequestParam String key, @RequestParam String value) {
        jedisService.set(key, value);
        return "sucess";

    }

    @GetMapping("/redis/{key}")
    public Object get(@PathVariable String key) {
        return jedisService.get(key);
    }

    @DeleteMapping("/redis/{key}")
    public Object delete(@PathVariable String key) {
        return jedisService.delete(key);
    }

}
